package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Alteration;
import visiopuce.objets.GeneNcbi;

public class GeneNcbiDAOImpl extends GenericDAOImpl<GeneNcbi, BigDecimal> implements GeneNcbiDAO {

	/** Constructeur priv� */
	private GeneNcbiDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static GeneNcbiDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static GeneNcbiDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new GeneNcbiDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public ArrayList<GeneNcbi> getGeneNCBIByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		ArrayList<GeneNcbi> genes = null;
		String sql = null;
		Query query;
		sql = "SELECT g FROM GeneNcbi g WHERE  g.id.chromosome LIKE :chromosome AND g.debutHg19 IS NOT NULL AND g.debutHg19 <= :fin AND g.finHg19 IS NOT NULL AND g.finHg19 >= :debut ORDER BY g.debutHg19";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		genes = (ArrayList<GeneNcbi>) query.list();
		return genes;
	}

	@Override
	public ArrayList<GeneNcbi> getImprintingByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		ArrayList<GeneNcbi> genes = null;
		String sql = null;
		Query query;
		sql = "SELECT g FROM GeneNcbi g WHERE g.debutHg19 IS NOT NULL AND g.debutHg19 <= :fin AND g.finHg19 IS NOT NULL AND g.finHg19 >= :debut AND g.id.chromosome LIKE :chromosome AND g.imprinting like 'oui' ORDER BY g.debutHg19";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		genes = (ArrayList<GeneNcbi>) query.list();
		return genes;
	}

	@Override
	public int getNbGeneNCBIByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		String sql = null;
		Query query;
		sql = "SELECT count(g.id.idNcbi) FROM GeneNcbi g WHERE g.debutHg19 IS NOT NULL AND g.debutHg19 <= :fin AND g.finHg19 IS NOT NULL AND g.finHg19 >= :debut AND g.id.chromosome LIKE :chromosome ORDER BY g.debutHg19";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		Long nbGenesl = (Long) query.uniqueResult();
		return nbGenesl.intValue();
	}

	@Override
	public GeneNcbi getGeneNCBIBySymbol(String symbol) {
		GeneNcbi gene = null;
		Query query;
		String sql = "SELECT g FROM GeneNcbi g WHERE g.symbol like :symbol";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("symbol", symbol);
		ArrayList<GeneNcbi> genes = (ArrayList<GeneNcbi>) query.list();
		if (genes != null && genes.size() > 0) {
			gene = genes.get(0);
		}
		return gene;
	}
}